package com.tianqu.tms.www.car.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang
 * 车辆搜索修改追加input 
 */
public class CarSearchUpdateInputController extends TmsController{

	private Logger logger = Logger
	.getLogger(CarSearchUpdateInputController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controllor = TxController.getTxController();
		// 添加事务提交异常捕获
		try {
			controllor.begin();
			CarCreationInputViewHelper carCreationInputViewHelper = new CarCreationInputViewHelper();
			carCreationInputViewHelper = (CarCreationInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			//取系统时间
			carCreationInputViewHelper.setuTime(DateUtil.getCurrentTime());//当前系统时间打到前台
			Car car = Car.getInstance();
			Codes code = Codes.getInstance();
			car = car.findCarBySerial(carCreationInputViewHelper.getSerial());
			if(car != null){
				carCreationInputViewHelper.setCarType(String.valueOf(car.getCarType()));
				carCreationInputViewHelper.setColor(String.valueOf( car.getColor()));
				carCreationInputViewHelper.setCarUse(String.valueOf( car.getCarUse()));
				carCreationInputViewHelper.setCardType(String.valueOf( car.getCardType()));
				carCreationInputViewHelper.setFuelType(String.valueOf( car.getFuelType()));
				carCreationInputViewHelper.setCountryStyle(String.valueOf( car.getCountryStyle()));
				carCreationInputViewHelper.setAttribute(String.valueOf(car.getAttribute()));
				carCreationInputViewHelper.setOwnerType(String.valueOf( car.getOwnerType()));
				carCreationInputViewHelper.setMemberId(String.valueOf(car.getMemberId()));
			}
						
			Member menber = Member.getInstance();
			carCreationInputViewHelper.setMemberList(menber.findAllMemInfo());//会员类型
			
			carCreationInputViewHelper.setCarTypeList(code.getCodeslistByItemId(Codes.VEHICLE_TYPE));//车辆类型
			carCreationInputViewHelper.setColorList(code.getCodeslistByItemId(Codes.VEHICLE_COLOR));//车辆颜色
			carCreationInputViewHelper.setCarUseList(code.getCodeslistByItemId(Codes.USER_NATURE));// 使用性质list
			carCreationInputViewHelper.setCardTypeList(code.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
			carCreationInputViewHelper.setFuelTypeList(code.getCodeslistByItemId(Codes.FUEL_TYPE));// 燃料种类list
			carCreationInputViewHelper.setCountryStyleList(code.getCodeslistByItemId(Codes.VEHICLE_COUNTRY));// 国别list
			carCreationInputViewHelper.setAttributeList(code.getCodeslistByItemId(Codes.ATTACHMENT_PROPERTY));// 车辆状态list' 
			carCreationInputViewHelper.setOwnerTypeList(code.getCodeslistByItemId(Codes.CAR_OWNER));// 车主类型list

			//车辆品牌
			Vendor vendor = Vendor.getInstance();
			VehicleBrand brand = VehicleBrand.getInstance();
			CarStyle carStyle = CarStyle.getInstance();
			carCreationInputViewHelper.setVendorList(vendor.findAllVendorInfo());// 车主类型list	
			carCreationInputViewHelper.setVendorId(brand.getVendorId(carCreationInputViewHelper.getBrandId()));
			carCreationInputViewHelper.setBrandList(brand.getAllVehicleBrandInfo(carCreationInputViewHelper.getVendorId()));
			carCreationInputViewHelper.setCarStyleList(carStyle.getAllCarStyleInfo(carCreationInputViewHelper.getBrandId()));	
			
			//获取图片信息
			//进入修改页面则从数据库中读出图片信息
			CarImage imgEntity = CarImage.getInstance();
			List<CarImage> list = null;
			if(uc.getAttribute("imgList")!=null)
			{
				list =(List<CarImage>)uc.getAttribute("imgList");
			}else
			{
				list = imgEntity.getCarImgInfoBySerial(carCreationInputViewHelper.getId());
			}
			if(list!=null)
			{
				for(int i=0;i<list.size();i++)
				{
					CarImage carImage = (CarImage)list.get(i);
					//从这里获取图片信息放到viewhelp中
					if(carImage.getImgKey().equals("1"))
					{
						carCreationInputViewHelper.setImg1(carImage.getImgPath());
					}
					if(carImage.getImgKey().equals("2"))
					{
						carCreationInputViewHelper.setImg2(carImage.getImgPath());
					}
					if(carImage.getImgKey().equals("3"))
					{
						carCreationInputViewHelper.setImg3(carImage.getImgPath());
					}
					if(carImage.getImgKey().equals("4"))
					{
						carCreationInputViewHelper.setImg4(carImage.getImgPath());
					}
					if(carImage.getImgKey().equals("5"))
					{
						carCreationInputViewHelper.setImg5(carImage.getImgPath());
					}
					if(carImage.getImgKey().equals("6"))
					{
						carCreationInputViewHelper.setImg6(carImage.getImgPath());
					}
					if(carImage.getImgKey().equals("7"))
					{
						carCreationInputViewHelper.setImg7(carImage.getImgPath());
					}
					if(carImage.getImgKey().equals("8"))
					{
						carCreationInputViewHelper.setImg8(carImage.getImgPath());
					}
				}
			}else
			{
				carCreationInputViewHelper.setImg1("");
				carCreationInputViewHelper.setImg2("");
				carCreationInputViewHelper.setImg3("");
				carCreationInputViewHelper.setImg4("");
				carCreationInputViewHelper.setImg5("");
				carCreationInputViewHelper.setImg6("");
				carCreationInputViewHelper.setImg7("");
				carCreationInputViewHelper.setImg8("");
			}
			uc.setAttribute(TmsConstants.VIEWHELP, carCreationInputViewHelper);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controllor.commit();
		} catch (Exception e) {
			try {
				controllor.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}
		
	

}
